﻿using System;
using System.Net.Http;
using CkSoftware.GroupMe.Sdk.Bot.Dao;
using CkSoftware.GroupMe.Sdk.Core;
using CkSoftware.GroupMe.Sdk.Core.Dao;

namespace CkSoftware.GroupMe.Sdk.Bot.Commands
{
	/// <summary>
	/// The post botmessage command (GroupMe documentation: <see href="https://dev.groupme.com/docs/v3#bots_post" />).
	/// </summary>
	public class PostBotMessageCommand : IWebApiCommand<GroupMeApiResponse<EmptyResponse>>
	{
		private readonly PostBotMessageParameters _parameters;

		/// <summary>
		/// Instantiates a new PostBotMessageCommand.
		/// </summary>
		/// <param name="botId">The id of the bot with which the message should be posted.</param>
		/// <param name="message">The message string.</param>
		public PostBotMessageCommand(string botId, string message)
		{
			_parameters = new PostBotMessageParameters {BotId = botId, Message = message};
		}

		/// <summary>
		/// The name of the command to be executed.
		/// </summary>
		public string CommandName
		{
			get { return "bots/post"; }
		}

		/// <summary>
		/// The http method to execute this command.
		/// </summary>
		public HttpMethod HttpMethod
		{
			get { return HttpMethod.Post; }
		}

		/// <summary>
		/// The parameters which should be used on command execution.
		/// </summary>
		public IWebApiCommandParameters Parameters
		{
			get { return _parameters; }
		}

		/// <summary>
		/// Defines if the command is executed once.
		/// </summary>
		public bool CommandExecuted { get; set; }

		/// <summary>
		/// Defines that the bot-message should contain the given attachment.
		/// </summary>
		/// <param name="attachment">The attachment to add to the bot-message.</param>
		/// <returns>The extended PostBotMessageCommand.</returns>
		/// <exception cref="ArgumentNullException">Occurs if the attachment is null.</exception>
		/// <exception cref="ArgumentException">Occurs if the attachment-type is not set.</exception>
		public PostBotMessageCommand WithAttachment(GroupMeAttachmentBase attachment)
		{
			if (attachment == null)
			{
				throw new ArgumentNullException("attachment");
			}

			if (attachment.Type == AttachmentType.Default)
			{
				throw new ArgumentException("The attachment type must be set correctly.", "attachment");
			}

			_parameters.Attachments.Add(attachment);
			return this;
		}
	}
}